package com.jkoss.wine.merchantSetting.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.util.Constant;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.merchantSetting.entity.MerchantAsset;
import com.jkoss.wine.merchantSetting.service.IMerchantAssetService;
import com.jkoss.wine.shop.service.IMerchantCommonTypeService;
import com.jkoss.wine.system.entity.MerchantEmployees;
import com.jkoss.wine.system.entity.Role;
import com.jkoss.wine.warehouse.entity.Warehouse;

/**
 * 前端控制器
 * 
 * @Author chair
 * @Version 1.0, 2018-10-01
 * @See
 * @Since com.jkoss.wine.merchantSetting.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/merchantSetting/merchantAsset")
public class MerchantAssetController extends BaseController {

	@Autowired
	private IMerchantCommonTypeService iMerchantCommonTypeService;
	@Autowired
	private IMerchantAssetService iMerchantAssetService;

	/**
	 * 列表
	 * 
	 * @param commonTypeId
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("/merchantSetting/merchantAsset/list")
	public String list(String commonTypeId, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {

		dwzPageBean.getCountResultMap().put("commonTypeId", commonTypeId);

		// 查找所有分组信息
		// 类型 0容量单位 (如ml ,l等) 1资源分组名称
		map.put("merchantCommonTypes", iMerchantCommonTypeService.selectList(Condition.create().eq("type_class", "1").ne("del", 1)));
		Wrapper wrapper = Condition.create().ne("ast.del", 1).and("ast.common_type_id = ct.common_type_id");
		if (!CommonUtil.isBlank(commonTypeId)) {
			wrapper.eq("ast.common_type_id", commonTypeId);
		}
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("add_time", false);
		}
		Page resultPage = iMerchantAssetService.selectVoPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "merchantSetting/merchantAsset/list";
	}

	/**
	 * 去新增
	 * 
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toInsert")
	@RequiresPermissions("/merchantSetting/merchantAsset/toInsert")
	public String toInsert(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		// 查找所有分组信息
		// 类型 0容量单位 (如ml ,l等) 1资源分组名称
		map.put("merchantCommonTypes", iMerchantCommonTypeService.selectList(Condition.create().eq("type_class", "1").ne("del", 1)));
		return "merchantSetting/merchantAsset/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/merchantSetting/merchantAsset/toInsert")
	@ResponseBody
	public Object insert(MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
		if (CommonUtil.isBlank(file) || CommonUtil.isBlank(file.getOriginalFilename())) {
			return ajaxError("文件不能为空");
		}
		try {
			File filePath = new File(Constant.IMAGES_PATH);
			if (!filePath.exists()) {
				filePath.mkdirs();
			}
			file.transferTo(new File(Constant.IMAGES_PATH + file.getOriginalFilename()));
			MerchantAsset merchant_asset = new MerchantAsset();
			merchant_asset.setCommonTypeId(request.getParameter("commonTypeId"));
			merchant_asset.setAssetUrl(Constant.IMAGES_URL + "/" + file.getOriginalFilename());
			merchant_asset.setPicName(file.getOriginalFilename());
			merchant_asset.setAddTime(CommonUtil.getDate());
			// 获取当前操作人，当操作人为null时，表示自动录入
			MerchantEmployees employees = (MerchantEmployees) request.getSession().getAttribute(Constant.SESSION_USER_KEY);
			merchant_asset.setOperator(employees.getEmployeeId());
			// 是否删除：0-未删除、1-已删除、2-不能删除
			merchant_asset.setDel(0);
			if (iMerchantAssetService.insert(merchant_asset)) {
				return ajaxSuccess();
			} else {
				return ajaxError();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ajaxError();
		}

	}

	/**
	 * 去修改
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toUpdate")
	@RequiresPermissions("/merchantSetting/merchantAsset/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		// 查找所有分组信息
		// 类型 0容量单位 (如ml ,l等) 1资源分组名称
		map.put("merchantCommonTypes", iMerchantCommonTypeService.selectList(Condition.create().eq("type_class", "1").ne("del", 1)));
		map.put("record", iMerchantAssetService.selectById(id));
		return "merchantSetting/merchantAsset/renamePic";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/merchantSetting/merchantAsset/toUpdate")
	@ResponseBody
	public Object update(@Valid MerchantAsset merchant_asset, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Wrapper wrapper = Condition.create().ne("del", 1).eq("pic_name", merchant_asset.getPicName()).ne("merchant_asset_id", merchant_asset.getMerchantAssetId());
		MerchantAsset merchantAsset = iMerchantAssetService.selectOne(wrapper);
		if (!CommonUtil.isBlank(merchantAsset)) {
			throw new Exception("图片名称已存在");
		}
		if (iMerchantAssetService.updateById(merchant_asset)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("/merchantSetting/merchantAsset/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		List list = new ArrayList();
		for (String string : id) {
			MerchantAsset merchantAsset = new MerchantAsset();
			merchantAsset.setMerchantAssetId(string);
			// 是否删除：0-未删除、1-已删除、2-不能删除
			merchantAsset.setDel(1);
			list.add(merchantAsset);
		}
		if (iMerchantAssetService.updateBatchById(list)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}
}
